<?php
/* @var $this MemberController */
/* @var $model Member */
$this->layout = "//layouts/column1";

$this->menu = array(
    array('label' => 'List Member', 'url' => array('index')),
        array('label' => 'Create Member', 'url' => array('create')),
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$('#member-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
");
?>

<h1>Manage Members</h1>

    <!--<p>
        You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
        or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
    </p>-->

<?php echo CHtml::link('Create Member', '/index.php/member/create'); ?><br>
<?php echo CHtml::link('Member Graph', '/index.php/member/membergraph'); ?><br>
<?php echo CHtml::link('Advanced Search', '#', array('class' => 'search-button')); ?>
<div class="search-form" style="display:none">
    <?php
    $this->renderPartial('_search', array(
        'model' => $model,
    ));
    ?>
</div><!-- search-form -->

<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'member-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(        
        'member_id',
        'username',
        'plain_password',
        'member_type',
        'parent_id',
        'test_account',
        'a_point',
        'b_point',
        'a_child_id',
        'b_child_id',
        'email',
        array(
            'header' => 'Reg Date',
            'value' => 'getFDate($data->create_date)',
        ),
        array(
            'header' => 'Balance',
            'value' => 'getCurrentBalance($data->member_id)',
        ),
        array(
            'header' => 'Deposits',
            'value' => 'getDeposit($data->member_id)',
        ),
        array(
            'header' => 'Earnings',
            'value' => 'getEarnings($data->member_id)',
        ),
        array(
            'header' => 'Withdrawn',
            'value' => 'getWithdrawn($data->member_id)',
        ),
        /*
          'security_question_id',
          'security_answer',
          'security_code',
          'upline_id',
          'agree_to_tos',
          'create_date',
          'create_by',
          'update_date',
          'update_by',
          'status',
          'active',
         */
        array(
            'class' => 'CButtonColumn',
        ),
    ),
));

function getFDate($date){
    $date = date_create($date);
    return date_format($date, 'Y-m-d');
} 

//function getCurrentBalance($id) {
//    $sql = "select * from member_transaction_history where member_transaction_history_id = (
//                    select max( member_transaction_history_id ) from member_transaction_history 
//                    where member_id =" . $id . ")";
//
//    $_lh = Yii::app()->db->createCommand($sql)->queryAll();
//    $balance = 0;
//    if (!empty($_lh)) {
//        $_history = $_lh[0];
//        if (!empty($_history)) {
//            $balance = $_history['balance'];
//        }
//    }
//    return $balance;
//}

function getCurrentBalance($id) {
    $credit = 0;
    $debit = 0;

    $sql = "select sum(credit) credit, sum(debit) debit from member_transaction_history where member_id = $id and (isDeposit is NULL or isDeposit <> '1')";
    $_lh = Yii::app()->db->createCommand($sql)->queryAll();
    if (!empty($_lh)) {
        $_history = $_lh[0];
        if (!empty($_history)) {
            $credit = $_history['credit'];
            $debit = $_history['debit'];
        }
    }
    return $credit - $debit;
}

function getDeposit($id) {
    $sum = Yii::app()->db->createCommand("SELECT SUM( debit ) AS sum FROM member_transaction_history 
        where member_id =$id and particulars like '%Deposit%' and payment_processors_id is not null")->queryScalar();
    if ($sum == "") {
        $sum = 0;
    }
    return $sum;
}

function getEarnings($id) {
    $sum = Yii::app()->db->createCommand("SELECT SUM( credit ) AS sum FROM member_transaction_history 
        where member_id =$id and payment_processors_id is null")->queryScalar();

    if ($sum == "") {
        $sum = 0;
    }
    return $sum;
}

function getWithdrawn($id) {
    $sum = Yii::app()->db->createCommand("SELECT SUM( debit ) AS sum FROM member_transaction_history 
        where member_id =$id and particulars not like '%Deposit%' and  payment_processors_id is not null")->queryScalar();

    if ($sum == "") {
        $sum = 0;
    }
    return $sum;
}
?>
